{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 变量"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "变量名不能是Python中的关键字。输出Python内置的关键字，如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n"
     ]
    }
   ],
   "source": [
    "import keyword\n",
    "\n",
    "list = keyword.kwlist\n",
    "# list\n",
    "print(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 多变量赋值\n",
    "\n",
    "# 方式1：\n",
    "a1 = b1 = c1 = 1\n",
    "print(c1)\n",
    "\n",
    "# 方式2：\n",
    "a2, b2, c3 = 1, True, 'TalkActor'\n",
    "print(b2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用type()函数来查看具体值的数据类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'float'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(type(1))\n",
    "print(type(1.1))\n",
    "print(type(\"abc\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 交互输入与输出格式设置\n",
    "## 输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "按下enter键后退出：爱抚\n",
      "你输入的数据是： 爱抚\n",
      "输入数据的类型是： <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入\n",
    "myInput = input('\\n按下enter键后退出：')\n",
    "print('你输入的数据是：',myInput)\n",
    "print('输入数据的类型是：', type(myInput))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "a123b"
     ]
    }
   ],
   "source": [
    "x = 'a'\n",
    "y = 'b'\n",
    "\n",
    "# 换行输出\n",
    "print(x)\n",
    "print(y)\n",
    "\n",
    "# 不换行输出\n",
    "print(x, end='123')\n",
    "print(y, end='')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TalkActor\n"
     ]
    }
   ],
   "source": [
    "# 标准输出\n",
    "import sys\n",
    "\n",
    "string = 'TalkActor'\n",
    "sys.stdout.write(string + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "item_one +  item_one + item_one + item_one\n"
     ]
    }
   ],
   "source": [
    "# 输出多行语句，用反斜杠 \\ 连接\n",
    "total = 'item_one +  item_one + ' \\\n",
    "        'item_one + item_one'\n",
    "        \n",
    "print(total)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 设置输出格式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用str.format()方法设置输出格式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我正在学习python\n"
     ]
    }
   ],
   "source": [
    "# 一对一填充\n",
    "print(\"我正在学习{}\".format(\"python\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我正在书房里学习python\n"
     ]
    }
   ],
   "source": [
    "# 多对多填充\n",
    "print(\"我正在{}学习{}\".format(\"书房里\",\"python\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018年中国单身人数约2.00亿\n"
     ]
    }
   ],
   "source": [
    "# 浮点数设置。 .2f表示以浮点型显示小数点后2位。\n",
    "print(\"{}年中国单身人数约{:.2f}亿\".format(2018,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中国男性占总人口的比例：51.90%\n"
     ]
    }
   ],
   "source": [
    "# 百分数设置。.2% 表示以百分比的形式展示小数点后两位。\n",
    "print(\"中国男性占总人口的比例：{:.2%}\".format(0.519))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串\n",
    "## 连接字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张小二小三\n"
     ]
    }
   ],
   "source": [
    "print(\"张\" + \"小二\" + \"小三\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 复制字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataAnalysis, DataAnalysis, DataAnalysis, \n"
     ]
    }
   ],
   "source": [
    "print(\"DataAnalysis, \" * 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取字符串的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "<class 'int'>\n",
      "11\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "print(len(\"110101199003073474\"))  # 身份证号\n",
    "print(type(len(\"110101199003073474\")))  # 获取类型\n",
    "\n",
    "print(len(\"18605221808\"))  # 手机号\n",
    "print(len(\"张小二小三\"))  # 姓名"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查找字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 测试字符串是否存在\n",
    "print(\"测试\" in \"新产品上线测试号\")  # True\n",
    "print(\"测试\" in \"我是一个正常用户号\")  # False\n",
    "\n",
    "print(\"测试\" not in \"新产品上线测试号\")  # False\n",
    "print(\"测试\" not in \"我是一个正常用户号\")  # True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "find()方法:  \n",
    "1、如果存在所查字符，则返回该字符具体位置；  \n",
    "2、如果不存在所查字符，则返回-1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str.find(\"saj\")  # 13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1\n"
     ]
    }
   ],
   "source": [
    "str = \"DSFDSfdsahfkdsajhfjk\"\n",
    "print(str.find(\"cdfgas\"))  # -1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python数据分析\n",
      "p\n",
      "析\n",
      "python数据分\n",
      "tho\n",
      "thon数据分析\n"
     ]
    }
   ],
   "source": [
    "string = \"python数据分析\"\n",
    "\n",
    "print(string)\n",
    "# 1、普通索引；\n",
    "print(string[0])\n",
    "print(string[-1])\n",
    "\n",
    "# 2、切片索引\n",
    "print(string[0:-1])\n",
    "print(string[2:5])\n",
    "print(string[2:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串的分隔"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用split()函数,将一个字符串用某个分隔符隔开；分隔后的值会以列表的形式返回。  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', ' bc', ' d', ' ef']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str = \"a, bc, d, ef\"\n",
    "# print(str.split(\",\"))\n",
    "str.split(\",\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 移除指定字符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用strip()方法来移除字符串**首尾**的指定字符；  \n",
    "默认移除字符串首尾的**空格**或**换行符**。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ahk fdshakh\n"
     ]
    }
   ],
   "source": [
    "# 移除空格\n",
    "str1 = \" ahk fdshakh  \"\n",
    "print(str1.strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\ta\t\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "str2 = \"\\ta\\t\" #\\t（制表符）\n",
    "print(str2)\n",
    "print(str2.strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dsfagdga'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 移除指定\n",
    "str3 = \"sdsfagdgas\"\n",
    "str3.strip(\"s\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表\n",
    "## 创建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "[1, 2, 'b', 'good']\n"
     ]
    }
   ],
   "source": [
    "# 创建一个空列表\n",
    "null_list = []\n",
    "print(null_list)\n",
    "\n",
    "# 建立一个int + str 类型的列表\n",
    "int_str_list = [1, 2, \"b\", \"good\"]\n",
    "print(int_str_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 复制列表 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n",
      "['a', 'b', 'v', 'a', 'b', 'v']\n"
     ]
    }
   ],
   "source": [
    "int_list = [1, 2, 3]\n",
    "print(int_list * 3)\n",
    "\n",
    "str_list = [\"a\", \"b\", \"v\"]\n",
    "print(str_list * 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 合并列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 'a', 'b', 'v']\n"
     ]
    }
   ],
   "source": [
    "# 方法1：使用 “+”操作符\n",
    "int_list = [1, 2, 3]\n",
    "str_list = [\"a\", \"b\", \"v\"]\n",
    "\n",
    "print(int_list + str_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 'a', 'b', 'v']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法2：使用extend()方法\n",
    "list_a = [1, 2, 3]\n",
    "list_b = [\"a\", \"b\", \"v\"]\n",
    "\n",
    "list_a.extend(list_b)\n",
    "list_a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 插入新元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "int_list = [1, 2, 3]\n",
    "\n",
    "# 向列表末尾插入新元素\n",
    "int_list.append(4)\n",
    "print(int_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4, 3]\n"
     ]
    }
   ],
   "source": [
    "int_list = [1, 2, 3]\n",
    "\n",
    "# 在索引2处，插入元素4\n",
    "int_list.insert(2, 4)  \n",
    "print(int_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 统计元素出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用count()方法获取某个值在列表中出现的次数。\n",
    "score_list = [\"一班\", \"一班\", \"三班\", \"二班\", \"一班\"]\n",
    "score_list.count(\"一班\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取指定元素的位置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "已知公司的所有销售业绩是按降序排列的，想看一下杨新竹的业绩排在第几。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "# 使用index()方法获取特定值的位置\n",
    "sale_list = [\"张达端\", \"李不菲\", \"侯晓霞\", \"赵东方\", \"杨欣欣\", \"白范德\"]\n",
    "print(sale_list.index(\"杨欣欣\") + 1)  # 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'f'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val = [\"f\", \"ds\", \"ds\", 1, \"v\"]\n",
    "\n",
    "# 普通索引: 获取某一特定位置的值\n",
    "val[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'v']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片索引： 获取某一位置区间内的值\n",
    "val[3:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除列表中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['f', 'd2', 1, 'v']\n",
      "['ds', 'd2', 1, 'v']\n"
     ]
    }
   ],
   "source": [
    "list_a = [\"f\", \"ds\", \"d2\", 1, \"v\"]\n",
    "list_b = [\"f\", \"ds\", \"d2\", 1, \"v\"]\n",
    "\n",
    "# pop()：删除指定位置的值\n",
    "list_a.pop(1) #删除索引1位置的值\n",
    "print(list_a)  \n",
    "\n",
    "# remove(): 删除指定元素\n",
    "list_b.remove(\"f\")\n",
    "print(list_b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 对列表中的值进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 4, 5, 6, 9, 12, 66]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  sort()方法能且只能实现升序排列\n",
    "int_list = [4, 6, 12, 9, 2, 1, 5, 66]\n",
    "int_list.sort()\n",
    "\n",
    "# 默认升序排列\n",
    "int_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'ads', 'asd', 'dsa', 'ikj', 't']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对字符串进行排序\n",
    "str_list = [\"asd\", \"ads\", \"ikj\", \"a\", \"t\", \"dsa\"]\n",
    "str_list.sort()\n",
    "str_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典\n",
    "字段(dict)是一种键值对结构，类似于通过联系人姓名查找联系人电话；  \n",
    "键必须是唯一的，就像如果有两个人同名，就无法找到正确的信息一样。  \n",
    "## 新建字典\n",
    "先创建一个空字典，然后向该字典内输入值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'张三': 13313581900, '李四': 13305231113}\n"
     ]
    }
   ],
   "source": [
    "# 方法1\n",
    "test_dict = {} # 创建一个空字典\n",
    "test_dict[\"张三\"] = 13313581900\n",
    "test_dict[\"李四\"] = 13305231113\n",
    "print(test_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'王五': 18605231115, '赵六': 18605231100}\n"
     ]
    }
   ],
   "source": [
    "# 方法2：将值直接以键值对的形式传入字典中\n",
    "phone_dict = {\"王五\": 18605231115, \"赵六\": 18605231100}\n",
    "print(phone_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取字典的键值 \n",
    "### keys()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['张三', '李四', '王五', '赵六'])\n"
     ]
    }
   ],
   "source": [
    "test_dict = {\"张三\": 18605291410, \"李四\":18605291600, \"王五\": 18605231115, \"赵六\": 18605231100}\n",
    "print(test_dict.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### values()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values([18605291410, 18605291600, 18605231115, 18605231100])\n"
     ]
    }
   ],
   "source": [
    "test_dict = {\"张三\": 18605291410, \"李四\":18605291600, \"王五\": 18605231115, \"赵六\": 18605231100}\n",
    "print(test_dict.values())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### item()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('张三', 18605291410), ('李四', 18605291600), ('王五', 18605231115), ('赵六', 18605231100)])\n"
     ]
    }
   ],
   "source": [
    "test_dict = {\"张三\": 18605291410, \"李四\":18605291600, \"王五\": 18605231115, \"赵六\": 18605231100}\n",
    "print(test_dict.items())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组\n",
    "元组内的元素不能被修改。\n",
    "## 新建元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "('a', 'b', 'c', 'd', 'e', 'fd')\n"
     ]
    }
   ],
   "source": [
    "# 整数元组\n",
    "int_tuple = (1, 2, 3)\n",
    "print(int_tuple)\n",
    "\n",
    "# 字符串元组\n",
    "str_tuple = (\"a\", \"b\", \"c\", \"d\", \"e\", \"fd\")\n",
    "print(str_tuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取元组的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "# 使用len函数获取元组的长度\n",
    "tuple = (1, 2, 3, 4)\n",
    "print(len(tuple))\n",
    "print(type(len(tuple)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取元组内的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "(2, 3)\n",
      "(1, 2, 3)\n",
      "(2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "tup1 = (1, 2, 3, 4, 5)\n",
    "\n",
    "# 普通索引\n",
    "print(tup1[2])\n",
    "\n",
    "# 切片索引\n",
    "print(tup1[1:3])\n",
    "print(tup1[:3])\n",
    "print(tup1[1:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数\n",
    "## 普通函数\n",
    "### 无返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我正在地铁上学Python\n",
      "我正在公交车上学Java\n"
     ]
    }
   ],
   "source": [
    "def learn_1(location, language):\n",
    "    print(\"我正在{}上学{}\".format(location, language))\n",
    "    \n",
    "learn_1(\"地铁\", \"Python\")\n",
    "learn_1(\"公交车\", \"Java\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 有返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我正在地铁上学Python\n",
      "我正在公交车上学Java\n"
     ]
    }
   ],
   "source": [
    "def learn_2(location, language):\n",
    "    doing = (\"我正在{}上学{}\".format(location, language))\n",
    "    return doing\n",
    "\n",
    "print(learn_2(\"地铁\", \"Python\"))\n",
    "print(learn_2(\"公交车\", \"Java\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "# 求和\n",
    "def ordinarySumFunction(x, y):\n",
    "    result = x + y\n",
    "    return result\n",
    "\n",
    "print(ordinarySumFunction(5, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 匿名函数\n",
    "Python使用lambda关键字来创建匿名函数。  \n",
    "使用方法如下：  \n",
    "- lambda arg1, arg2, arg3: expression  \n",
    "arg1, arg2, arg3 表示具体的参数，expression表示参数要执行的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用匿名函数求和\n",
    "lambdaSumFunction = lambda x, y: x+y\n",
    "lambdaSumFunction(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55\n",
      "read: 0.000553秒\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "start = time.clock()\n",
    "fib = lambda n,x=0,y=1: x if not n else fib(n-1, y, x+y)\n",
    "print (fib(10))\n",
    "end = time.clock()\n",
    "\n",
    "print(\"read: %f秒\" %(end-start))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6765\n",
      "read: 0.003563秒\n"
     ]
    }
   ],
   "source": [
    "start = time.clock()\n",
    "fib = lambda n: 1 if n<=2 else fib(n-1)+fib(n-2)\n",
    "print (fib(20))\n",
    "end = time.clock()\n",
    "\n",
    "print(\"read: %f秒\" %(end-start))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 高级特性\n",
    "## 列表生成式\n",
    "### 求平方 \n",
    "已有一个列表，要求对该列表中的每个值求平方，然后将结果组成一个新列表\n",
    "**普通方法：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36]\n"
     ]
    }
   ],
   "source": [
    "int_list = [1, 2, 3, 4, 5, 6]\n",
    "new_int_list = []  # 创建一个空列表来存放计算后的结果\n",
    "\n",
    "for i in int_list:\n",
    "    new_int_list.append(i ** 2)\n",
    "    \n",
    "print(new_int_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**列表生成式：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36]\n"
     ]
    }
   ],
   "source": [
    "int_list = [1, 2, 3, 4, 5, 6]\n",
    "\n",
    "new_int_list = [i**2 for i in int_list]\n",
    "\n",
    "print(new_int_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 两两组合\n",
    "有两个字符串列表，把这两个列表中的值两两组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Aw', 'Ad', 'AD', 'ffw', 'ffd', 'ffD', 'dsw', 'dsd', 'dsD']\n"
     ]
    }
   ],
   "source": [
    "str_list_a = [\"A\", \"ff\", \"ds\"]\n",
    "str_list_b = [\"w\", \"d\", \"D\"]\n",
    "\n",
    "# 普通方法\n",
    "new_str_list_a = []\n",
    "\n",
    "for m in str_list_a:\n",
    "    for n in str_list_b:\n",
    "        new_str_list_a.append(m + n)\n",
    "        \n",
    "print(new_str_list_a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Aw', 'Ad', 'AD', 'ffw', 'ffd', 'ffD', 'dsw', 'dsd', 'dsD']\n"
     ]
    }
   ],
   "source": [
    "str_list_a = [\"A\", \"ff\", \"ds\"]\n",
    "str_list_b = [\"w\", \"d\", \"D\"]\n",
    "\n",
    "# 列表生成式\n",
    "new_str_list_b = [m + n for m in str_list_a for n in str_list_b]\n",
    "print(new_str_list_b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## map函数\n",
    "表现形式map(function, args)  \n",
    "意思是对序列args中的每个值进行function 操作， 最终得到一个结果序列。  \n",
    "不能直接把map函数生成的结果序列显示出来，需要通过for循环来遍历出结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7, 16, 27, "
     ]
    }
   ],
   "source": [
    "# 方法1：通过for循环来遍历出结果\n",
    "\n",
    "result_map = map(lambda x, y: x*y, [1, 2, 3], [7, 8, 9])\n",
    "# print(result_map)\n",
    "for i in result_map:\n",
    "    print(i, end=\", \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8, 10, 12]\n"
     ]
    }
   ],
   "source": [
    "# 方法2：使用list()方法，将结果值转换成一个列表\n",
    "result_map = map(lambda x, y: x+y, [1, 2, 3], [7, 8, 9])\n",
    "result_list = list(result_map)\n",
    "print(result_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## apply()函数\n",
    "apply()函数用于对DataFrame中某一column或row中的元素执行相同的函数操作。  \n",
    "apply()函数要与lambda函数结合使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "a93 = np.arange(9).reshape(3,3) + 1\n",
    "a93"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C1</th>\n",
       "      <th>C2</th>\n",
       "      <th>C3</th>\n",
       "      <th>C4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   C1  C2  C3  C4\n",
       "0   1   2   3   3\n",
       "1   4   5   6   6\n",
       "2   7   8   9   9\n",
       "3   7   8   9   9\n",
       "4   1   1   1   1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df93 = pd.read_excel(r\"..\\..\\dataSourceFiles\\test.xlsx\", sheet_name=\"apply\")\n",
    "df93"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2\n",
       "1    5\n",
       "2    8\n",
       "3    8\n",
       "4    2\n",
       "Name: C1, dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对C1列中的每一个元素加1\n",
    "df93[\"C1\"].apply(lambda x: x+1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## applymap()函数\n",
    "applymap()函数用于对DataFrame中的每一个元素执行相同的操作；  \n",
    "applymap()函数要与lambda函数结合使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C1</th>\n",
       "      <th>C2</th>\n",
       "      <th>C3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   C1  C2  C3\n",
       "0   1   2   3\n",
       "1   4   5   6\n",
       "2   7   8   9"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df94 = pd.read_excel(r\"..\\..\\dataSourceFiles\\test.xlsx\", sheet_name=\"apply\")\n",
    "df94"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C1</th>\n",
       "      <th>C2</th>\n",
       "      <th>C3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>16</td>\n",
       "      <td>25</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>49</td>\n",
       "      <td>64</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   C1  C2  C3\n",
       "0   1   4   9\n",
       "1  16  25  36\n",
       "2  49  64  81"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df94.applymap(lambda x: x*x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## zip()函数\n",
    "将可迭代的对象（列表、元组）作为参数，  \n",
    "将对象中对应的元素打包成一个个元组；  \n",
    "然后返回由这些元组组成的列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 'a')\n",
      "(2, 'b')\n",
      "(3, 'c')\n",
      "(4, 'd')\n",
      "(5, 'e')\n"
     ]
    }
   ],
   "source": [
    "# 当可迭代元素是列表时：\n",
    "int_list = [1, 2, 3, 4, 5]\n",
    "str_list = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"]\n",
    "\n",
    "for i in zip(int_list, str_list):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 'a')\n",
      "(2, 'b')\n",
      "(3, 'c')\n",
      "(4, 'd')\n",
      "(5, 'e')\n"
     ]
    }
   ],
   "source": [
    "# 当可迭代元素是元组时：\n",
    "int_tuple = (1, 2, 3, 4, 5)\n",
    "str_tuple = (\"a\", \"b\", \"c\", \"d\", \"e\", \"heh\")\n",
    "\n",
    "for i in zip(int_tuple, str_tuple):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元组与列表相互转换\n",
    "元组和列表是两种相似的数据结构，两者之间可以相互转换。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "['a', 'b', 'c', 'd', 'e', 'fd']\n"
     ]
    }
   ],
   "source": [
    "int_tuple = (1, 2, 3)\n",
    "str_tuple = (\"a\", \"b\", \"c\", \"d\", \"e\", \"fd\")\n",
    "\n",
    "# 使用函数list()将元组转换为列表\n",
    "print(list(int_tuple))\n",
    "print(list(str_tuple))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int_list = [1, 2, 3]\n",
    "str_list = [\"a\", \"b\", \"c\", \"d\", \"e\", \"fd\"]\n",
    "type(int_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "('a', 'b', 'c', 'd', 'e', 'fd')\n"
     ]
    }
   ],
   "source": [
    "# 使用函数tuple()将列表转换为元组\n",
    "print(tuple(int_list))\n",
    "print(tuple(str_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "307px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
